package com.pudu.mydemo.act;


import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;

import com.pudu.mydemo.utils.SeekBarVolumizer;
import com.pudu.mydemo.utils.SystemBrightManager;
import com.pudu.pudusclient.MainActivity;
import com.pudutech.pdrobot.R;


/**
 * Created by guiliu on 2016/7/12.
 */
public class SettingActivity extends BaseHomeActivity implements View.OnClickListener{
    private ImageView debug;
    private ImageButton back;
    private RelativeLayout wifiLogin;
    private SeekBar sun,sound;
    private static int currentScreenBrightness;
    private View rootView;
    private   SeekBarVolumizer seekBarVolumizer;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*if (savedInstanceState == null) {
            rootView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    rootView.getViewTreeObserver().removeOnPreDrawListener(this);
                    startRootAnimation();
                    return true;
                }
            });
        }*/
    }

    @Override
    protected int getLayoutId() {
        return R.layout.setting;
    }

    @Override
    public void initView() {
        rootView = findViewById(R.id.root);
        debug = (ImageView)findViewById(R.id.engineer_debug);
        wifiLogin = (RelativeLayout)findViewById(R.id.wifi_layout);
        back = (ImageButton) findViewById(R.id.back);
        sun = (SeekBar)findViewById(R.id.sun);
        sound = (SeekBar)findViewById(R.id.sound);
        sun.setMax(255);
    }

    private void startRootAnimation() {
        rootView.setScaleY(0.1f);
        rootView.setPivotY(rootView.getY() + rootView.getHeight() / 2);

        rootView.animate()
                .scaleY(1)
                .setDuration(1000)
                .setInterpolator(new AccelerateInterpolator())
                .start();
    }


    @Override
    protected void onResume() {
        super.onResume();
      //  initSound();
        initSun();
         seekBarVolumizer = new SeekBarVolumizer(this, sound,AudioManager.STREAM_MUSIC);
    }


    private void initSun()
    {
        currentScreenBrightness = SystemBrightManager.getSystemBrightness(this);
        Log.e("setting","currentScreenBrightness:"+currentScreenBrightness);
        //文本、进度条显示
        sun.setProgress(currentScreenBrightness);

    }

    @Override
    public void initEvent() {

        debug.setOnClickListener(this);
        back.setOnClickListener(this);

        wifiLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });


        // BORDER
       sun.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//                setScreenBrightness((float)progress/100);
                if(fromUser) {
                    if(SystemBrightManager.isAutoBrightness(SettingActivity.this)) {
                        Log.e("setting","changemode");
                        SystemBrightManager.setBrightnessMode(SettingActivity.this,android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
                    }
                    Log.e("setting","fromuser");
                    setBright(progress);
                }
                Log.e("setting","why.");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.engineer_debug:
                Intent intent = new Intent(SettingActivity.this, MainActivity.class);
                startActivity(intent);
                break;
            case R.id.back:
                finish();
                break;
        }
    }

/*

    public void setScreenBrightness(float progress) {
        // TODO Auto-generated method stub
        Log.e("setting","progress:"+progress);
        WindowManager.LayoutParams layoutparam=getWindow().getAttributes();//取得窗口属性
        layoutparam.screenBrightness=progress;//因为1-100的拖动条，缩小100倍0,0.01，0.02....0.1,0.2....1
        super.getWindow().setAttributes(layoutparam);

    }
*/

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(seekBarVolumizer == null) return true;
        boolean isdown = (event.getAction() == KeyEvent.ACTION_DOWN);
        Log.e("setting","isdown:"+isdown);
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                if(isdown)
                seekBarVolumizer.changeVolumeBy(1);
                return true;
            case KeyEvent.KEYCODE_VOLUME_UP:
                if(isdown)
                seekBarVolumizer.changeVolumeBy(-1);
                return true;
            default:
                return super.onKeyDown(keyCode, event);
        }

    }


    public void setBright(int progress) {
      boolean isAutoBrightness = SystemBrightManager.isAutoBrightness(this);
       Log.e("setting","thee brightValue:"+SystemBrightManager.getBrightness(this));
      if (isAutoBrightness) {  // 自动调整亮度
          SystemBrightManager.setBrightness(this, -1);
      } else {
//          int brightValue = SystemBrightManager.getBrightness(this);
//          SystemBrightManager.setBrightness(this, progress);
          SystemBrightManager.setSystemBrightness(this,progress);
      }
  }

}
